#!/usr/bin/python
# -*- coding: utf-8 -*-

# This file is part of Cockpit.
#
# Copyright (C) 2013 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.

import parent
from testlib import *

class TestShutdownRestart(MachineCase):
    additional_machines = {
        'machine2': { }
    }

    def setUp(self):
        # we need a static ip for this test
        MachineCase.setUp(self, macaddr='52:54:00:9e:00:F3')

        self.machine.execute("hostnamectl set-hostname machine1")
        self.machines['machine2'].execute("hostnamectl set-hostname machine2")

    def testBasic(self):
        m = self.machine
        b = self.browser

        m2 = self.machines['machine2']
        b2 = self.new_browser(m2.address)

        # HACK -https://bugzilla.redhat.com/show_bug.cgi?id=1278287
        m.execute("! selinuxenabled || setenforce 0")

        self.login_and_go("/system")

        # Reboot
        m.reset_reboot_flag()
        b.click("#shutdown-group button.btn-danger")
        b.wait_popup("shutdown-dialog")
        b.wait_in_text("#shutdown-dialog .btn-danger", 'Restart')
        b.click("#shutdown-dialog .dropdown button")
        b.click("a:contains('No Delay')")
        b.click("#shutdown-dialog .btn-danger")
        b.switch_to_top()

        b.wait_visible(".curtains-ct")
        b.wait_in_text(".curtains-ct h1", "Disconnected")

        m.wait_reboot()
        m.start_cockpit()
        b.click("#machine-reconnect")
        b.expect_load()
        b.wait_visible("#login")

        self.allow_restart_journal_messages()
        self.allow_journal_messages("Shutdown scheduled for .*")
        self.check_journal_messages()

        m2.start_cockpit()
        b2.login_and_go("/system")

        if not m.image in ["continuous-atomic", "fedora-atomic", "rhel-atomic"]:
            # Add machine with static IP to m2
            b2.switch_to_top()
            b2.go("/@%s" % m.address)
            b2.wait_visible("#machine-troubleshoot")
            b2.click('#machine-troubleshoot')
            b2.wait_popup('troubleshoot-dialog')
            b2.wait_present("#troubleshoot-dialog .btn-primary:not([disabled])")
            b2.wait_text('#troubleshoot-dialog .btn-primary', "Add")
            b2.click('#troubleshoot-dialog .btn-primary')
            b2.wait_in_text('#troubleshoot-dialog', "Fingerprint")
            b2.click('#troubleshoot-dialog .btn-primary')
            b2.wait_popdown('troubleshoot-dialog')
            b2.enter_page("/system", host=m.address)
            b2.wait_text("#system_information_hostname_button", "machine1")

            # Check auto reconnect on restart
            m.reset_reboot_flag()
            b2.click("#shutdown-group button.btn-danger")
            b2.wait_popup("shutdown-dialog")
            b2.wait_in_text("#shutdown-dialog .btn-danger", 'Restart')
            b2.click("#shutdown-dialog .dropdown button")
            b2.click("a:contains('No Delay')")
            b2.click("#shutdown-dialog .btn-danger")
            b2.switch_to_top()

            b2.wait_visible(".curtains-ct")
            b2.wait_visible(".curtains-ct .spinner")
            b2.wait_in_text(".curtains-ct h1", "restarting")

            m.wait_reboot()

            b2.wait_not_visible(".curtains-ct")
            b2.enter_page("/system", host=m.address, reconnect=False)

        # Power off with invalid date
        self.login_and_go("/system")
        b.enter_page("/system")
        b.click("#shutdown-group span.caret")
        b.click("#shutdown-group a:contains('Shut Down')")
        b.wait_popup("shutdown-dialog")
        b.click("#shutdown-dialog .dropdown button")
        b.click("a:contains('Specific Time')")
        b.wait_not_val("#shutdown-dialog input.shutdown-hours", "")
        old = b.val("#shutdown-dialog input.shutdown-hours")
        b.set_val("#shutdown-dialog input.shutdown-hours", "blah")
        b.click("#shutdown-dialog .btn-danger")
        b.wait_present("#shutdown-dialog .dialog-error")
        b.wait_text("#shutdown-dialog .dialog-error", "Invalid time format")

        # Now set a correct time and power off
        b.set_val("#shutdown-dialog input.shutdown-hours", old)
        b.click("#shutdown-dialog .btn-danger")
        b.switch_to_top()

        # we don't need to wait for the dialog to close here, just the disconnect
        b.wait_visible(".curtains-ct")
        b.wait_in_text(".curtains-ct h1", "Disconnected")

        m.wait_poweroff()

if __name__ == '__main__':
    test_main()
